<template>
	<view>

		<!-- 404页面 -->
		<view class="noData" v-if="netWorkType == 'none'">
			<noData @handle="refresh"></noData>
		</view>
		
		<view style="width:100%;" v-if="netWorkType != 'none'">
			<view class="list">
				<view class="text-box step">
					<view class="tit">填写信息</view>
					<view class="iconfont text">&#xe626;</view>
				</view>
				<view  class="text-box">
					<view class="tit">医生开方</view>
					<view class="iconfont">&#xe626;</view>
				</view>
				<view  class="text-box">
					<view class="tit">下单支付</view>
					<view class="iconfont">&#xe626;</view>
				</view>
				<view  class="text-box">
					<view class="tit">等待收货</view>
				</view>
			</view>
			<!-- 头部结束 -->
			
			<view class="center-box">
				
				<view class="tip">
					<!-- 提示 -->
					<view class="title">
						你的信息将被严格保密，仅开方使用
					</view>
					<view class="text">
						处方药需凭方购买，补充信息后医生将为你开具处方
					</view>
				</view>
				
				<view class="use-user margin-top20">
					<!-- 用药人 -->
					<view class="user-check">
						<view class="title">
							请选择用药人
						</view>
						<view class="add-btn"  @tap.stop="addFamily">
							<text  class="iconfont">&#xe61c;</text>
							添加
						</view>
					</view>
					<!-- 用药人 -->
					<view class="use-users">
						<!-- <view class="use-item use-check-user">
							<view class="name">
								<view>张三</view>
								<view ><text class="iconfont"> &#xe629;</text></view>
							</view>
							<view class='txt'>
								男  24岁
							</view>
						</view>
						
						<view class="use-item">
							<view class="name">
								<view>李四</view>
								<view ><text class="iconfont"> &#xe629;</text></view>
							</view>
							<view class='txt'>
								男  24岁
							</view>
						</view> -->
						<view  :class="useUsersId==item.id?'use-item use-check-user':'use-item'" v-for="item in data.patientList"  @tap="checkedUseUser(item)">
							<view class="name">
								<view>{{item.name}}</view>
								<view  @tap.stop="editPatient(item)"><text class="iconfont"> &#xe629;</text></view>
							</view>
							<view class='txt'>
								{{item.sex}}  {{item.age}}岁
							</view>
						</view>
						
						
					</view>
				</view>
				<view class="product-box margin-top20">
					<view class="title">
						请选择线下已确诊的疾病
					</view>
					<view class="title-remark">
						请选择线下已确诊的疾病
					</view>
					<view class="product">
						<view class="cover-image">
							<image :src="data.product.cover_image" mode="aspectFit" ></image>
						</view>
						<view class="product-name">
							{{data.product.name}}
						</view>
					</view>
					<view class="check-box">
						
						<view class="item"  v-for=" item,key in data.labelList" @tap.stop="checkedBz(item,key)" :class="item.checked?'checked':''" >
							{{item.name}}
							<text class="iconfont right-top" v-if="item.type==2" @tap.stop="delbz(key)">&#xe621;</text>
						</view>
						<view class="item" @tap.stop="$refs.addBingzheng.open()">
							<text class="iconfont">&#xe61c;</text> 添加
						</view>
					</view>
				</view>
				
				<view class="history-box margin-top20" @tap.stop="checkBingshi">
					<view>
						疾病史/妊娠哺乳
					</view>
					<view>		
						<span v-if="youBingshi">有病史</span>
						<text class="iconfont">&#xe626;</text>
					</view>
				</view>
				
				<view class="remark-box margin-top20" >
					<view class="remark-item">
						<view>主诉</view>
						<view class="iconfont">&#xe626;</view>
					</view>
					<view class="input-box">
						<!-- <input type="textarea" row='3' placeholder="请简单描述你的就诊原因"> -->
						<textarea class="text-area" v-model="remark" placeholder="请简单描述你的就诊原因"></textarea>
					</view>
				</view>
				
				<view class="fee margin-top20">
					<view>费用类型</view>
					<view>
						自费
					</view>
				</view>
				
				<view class="btn " @tap="toNext">
					提交并开方
				</view>
			</view>
			
			
		</view>
		<uni-popup ref="addBingzheng" type="center" custom="true" >
			<view class="center-bz">
				<view class="title">
					其他病症
				</view>
				<view>
					<input type="text" v-model="bz_name" placeholder="请输入">
				</view>
				<view class="footer">
					<!-- <view @tap="$refs.addBingzheng.close()" class="item">取消</view> -->
					<view @tap="addbz" class="item">确认</view>
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="checkHistory" type="bottom" custom="true" >
			<view class="history-tmp">
				<view class="title padding16">
					疾病史/妊娠哺乳
				</view>
				<view class="bingli padding16" >
					<view class="item-box">
						<view>肝功能</view>
						<view class="item-checked ">
							<view @tap.stop="checkBingLi('gan',2)" class="margin-right20">
								正常<text class="iconfont" :class="caseHistory.gan==2?'checked':'check'" >&#xe61e;</text>
							</view>
							<view @tap.stop="checkBingLi('gan',1)">
								异常<text class="iconfont" :class="caseHistory.gan==1?'checked':'check'" >&#xe61e;</text>
								
							</view>
							
						</view>
					</view>
					<!-- <view v-if="caseHistory.gan==1">
						<input type="text" placeholder="请输入(选填)">
					</view> -->
				</view>
				<view class="bingli padding16" >
					<view class="item-box">
						<view>肾功能</view>
						<view  class="item-checked ">
							<view @tap.stop="checkBingLi('shen',2)" class="margin-right20">
								正常<text class="iconfont" :class="caseHistory.shen==2?'checked':'check'" >&#xe61e;</text>
							
							</view>
							<view @tap.stop="checkBingLi('shen',1)">
								异常<text class="iconfont" :class="caseHistory.shen==1?'checked':'check'" >&#xe61e;</text>
								
							</view>
						</view> 
					</view>
					<!-- <view v-if="caseHistory.shen==1">
						<input type="text" placeholder="请输入(选填)">
					</view> -->
				</view>
				<view class="bingli padding16" >
					<view class="item-box">
						<view>用药史</view>
						<view class="item-checked">
							<view @tap.stop="checkBingLi('used',1)" class="margin-right20">
								有<text class="iconfont" :class="caseHistory.used==1?'checked':'check'" >&#xe61e;</text>
								
							</view>
							<view @tap.stop="checkBingLi('used',2)">
								无<text class="iconfont" :class="caseHistory.used==2?'checked':'check'" >&#xe61e;</text>
							</view>
							
						</view>
					</view>
					<view v-if="caseHistory.used==1">
						<input type="text" v-model="caseHistory.medicationHistory" placeholder="请输入你的用药史">
					</view>
				</view>
				<view class="bingli padding16" >
					<view class="item-box">
						<view>家庭病史</view>
						<view class="item-checked">
							<view @tap.stop="checkBingLi('history',1)" class="margin-right20">
								有<text class="iconfont" :class="caseHistory.history==1?'checked':'check'" >&#xe61e;</text>
							</view>
							<view @tap.stop="checkBingLi('history',2)">
								无<text class="iconfont"  :class="caseHistory.history==2?'checked':'check'" >&#xe61e;</text>
							</view>
							
						</view>
					</view>
					<view v-if="caseHistory.history==1">
						<input type="text" v-model="caseHistory.familyHistory" placeholder="请输入你的家族病史">
					</view>
				</view>
				<view class="bingli padding16" >
					<view class="item-box">
						<view>过敏史</view>
						<view class="item-checked">
							<view @tap.stop="checkBingLi('guomi',1)" class="margin-right20">
								有<text class="iconfont" :class="caseHistory.guomi==1?'checked':'check'" >&#xe61e;</text>
							</view>
							<view @tap.stop="checkBingLi('guomi',2)">
								无<text class="iconfont" :class="caseHistory.guomi==2?'checked':'check'" >&#xe61e;</text>
							</view>
							
						</view>
					</view>
					<view v-if="caseHistory.guomi==1">
						<input type="text" v-model="caseHistory.guoMinDetail" placeholder="请输入你的过敏史">
					</view>
				</view>
				<view class="bingli padding16" >
					<view class="item-box">
						<view>既往病史</view>
						<view class="item-checked">
							<view @tap.stop="checkBingLi('old',1)" class="margin-right20">
								有<text class="iconfont" :class="caseHistory.old==1?'checked':'check'" >&#xe61e;</text>
							</view>
							<view @tap.stop="checkBingLi('old',2)">
								无<text class="iconfont" :class="caseHistory.old==2?'checked':'check'" >&#xe61e;</text>
							</view>
						 	
						</view>
					</view>
					<view v-if="caseHistory.old==1">
						<input type="text" v-model="caseHistory.problemHistory"  placeholder="请输入你的既往病史">
					</view>
				</view>
				<view class="btn" @tap="$refs.checkHistory.close()">
						确认
				</view>
				
			</view>
		</uni-popup>
	</view>
</template>
<script>
	//公共api接口封装方法
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	import store from '@/store/index.js';
	//获取网络状态
	import {
		mapGetters
	} from 'vuex';
	export default {
		data() {
			return {
				isSelect: false,
				//网络状态
				networkStatus: true,
				//接口数据
				data: {
					addressList: [],
					patientList:[],
					product:[],
					labelList:[],
				},
				// 用药人
				useUsersId:'',
				remark:'',
				// 病史
				caseHistory:{
					gan:'',					
					
					shen:'',
					
					guomi:'',
					guoMinDetail:'',
					
					old:'',
					problemHistory:'',
					
					history:'',
					familyHistory:'',
					
					used:'',
					medicationHistory:'',
					
					
				},
				
				//症状
				bingzheng:[
					{
						type:1,//系统自带  2手动添加可移除
						name:'痤疮',
						checked:false,
					},
					{
						type:1,//系统自带  2手动添加可移除
						name:'结节性痤疮',
						checked:false,
					}
					
					
				],
				bz_name:'',
				product:'',
				num:'',
				norms_value_id:'',
				id:'',
				history:2,
				checkBzList:{
					
				},
				youBingshi:false,
			};
		},
		
		components: {
			uniPopup
		},
		computed: {
			//注册网络状态
			...mapGetters(['netWorkType','loginStatus'])
		},
		onShow() {
			
		},
		onLoad(e) {
			if(typeof e.product !='undefined'){
				this.product = e.product ;//json字符串 [{}]
			}
			if(this.product !=''){
				// product=[{"id":"134","norms_value_id":166,"num":1,"store_id":33,"store_name":"瓯宁","price":"29.00"}]
				for(let item of JSON.parse(this.product))
				{
					
					if(typeof item.norms_value_id !='undefined'){
						this.norms_value_id = item.norms_value_id;
						
					}
					if(typeof item.num !='undefined'){
						this.num = item.num
					}
				}
			}
			this.getList()
			
		},
		methods: {
			// 刷新的方法
			refresh() {
				this.getList()
			},
			getList() {
				//初始常用地址
				const _this = this
				console.log(66)				
				var opt = {
					url: '/inquiry/getList',
					data:{
						norms_value_id:this.norms_value_id||'171'
					},
					success: function(res) {
						//console.log(res)
						if (res.status == 1) {
							_this.data.product = res.data.product
							_this.data.labelList = res.data.labelList
							_this.data.patientList = res.data.patientList
							//console.log(_this.data.goods)
						} else {
							uni.showToast({
								icon: 'none',
								title: res.message
							});
						}

					}
				};
				_this.$service.GetApi(opt)


			},
			checkBingshi(){
				let _this = this
				console.log(1111)
				this.$refs.checkHistory.open()
			},
			// 病史选择
			checkBingLi(temp,v){
				this.caseHistory[temp] =v
				 let arr= Object.values(this.caseHistory)
				this.youBingshi=arr.indexOf(1) !=-1?true:false;
				console.log(arr.indexOf(1),this.youBingshi)
				// this.youBingshi=this.caseHistory.indexOf()
				
			},
			// 选择用药人
			checkedUseUser(item){
				this.useUsersId = item.id
			},
			// 添加用药人 
			addFamily(){
				const _this = this
				// uni.navigateTo({
				// 	url: "./edit/edit?type=add"
				// })
				// console.log(777)
				_this.$refer.setVilit('/packageRx/pages/add/add')
				let callback = function(url) {
					_this.$refs.popupvLogin.open()
				}
				var data = {
					type: 'add'
				}
				_this.$refer.toUrl(callback, data)
	
			},
			editPatient(item){
				const _this = this
				// uni.navigateTo({
				// 	url: "./edit/edit?type=add"
				// })
				_this.$refer.setVilit('/packageRx/pages/add/add')
				let callback = function(url) {
					_this.$refs.popupvLogin.open()
				}
				var data = {
					type: 'edit',
					patient_id:item.id,
				}
				_this.$refer.toUrl(callback, data)
			},
			// 选择病症
			checkedBz(item,key){
				 
				this.data.labelList.forEach((v,k)=>{
					if(k==key){
						v.checked = v.checked?false:true;
					}
				})
				
				// console.log(this.bingzheng)
			},
			// 添加其他病症
			addbz(){
				let temp={
					type:2,
					name:this.bz_name,
					checked:true
				}
				this.data.labelList.push(temp)
				this.bz_name ='';
				this.$refs.addBingzheng.close()
			},
			// 删除病症
			delbz(key){
				this.data.labelList.splice (key,1)
			},
			// 下一步医生开发
			toNext(){
				const _this = this
				// uni.navigateTo({
				// 	url: "./edit/edit?type=add"
				// })
				let checkLabel ='';
				_this.data.labelList.forEach((v,k)=>{
					if(v.checked){
						checkLabel= checkLabel+v.name+','
					}
				})
				
				_this.$refer.setVilit('/packageRx/pages/DoctorDrug/index')
				let callback = function(url) {
					_this.$refs.popupvLogin.open()
					return
				}
				if(!checkLabel){
					uni.showToast({
						icon:'none',
						title:'请选择线下已诊病例'
					})
					retrun ;
				}
				if(!_this.useUsersId){
					uni.showToast({
						icon:'none',
						title:'请选择用药人'
					})
					retrun ;
				}
				if(!this.caseHistory.guomi || !this.caseHistory.old ||!this.caseHistory.old || !this.caseHistory.history||!this.caseHistory.history||!this.caseHistory.shen)
				{
					uni.showToast({
						icon:'none',
						title:'请选择填写病史'
					})
					retrun ;
				}
				if(!_this.remark){
					uni.showToast({
						icon:'none',
						title:'请输入病症主诉'
					})
					retrun ;
				}
				
				//  病史状态统一 1 异常 2正常
				var data = {
					patient_id:_this.useUsersId,//用药人
					norms_value_id:_this.norms_value_id,//产品规格id
					checkLebal:checkLabel,//病症
					chiefComplaint:_this.remark,//主诉
					guoMinDetail:this.caseHistory.guomi ==1?this.caseHistory.guoMinDetail:'无',//过敏史
					problemHistory:this.caseHistory.old ==1?this.caseHistory.problemHistory:'无',//既往病史
					medicationHistory:this.caseHistory.used ==1?this.caseHistory.medicationHistory:'无',//用药史
					familyHistory:this.caseHistory.history ==1?this.caseHistory.familyHistory:'无',//家庭病史
					liverFunction:this.caseHistory.gan ==1?'异常':'正常',//肝
					renalFunction:this.caseHistory.shen ==1?'异常':'正常',//肾
					payType:'自费',
					num:this.num,//购买数量
					product:_this.product,//字符串 
					
				}
				
				_this.$refer.toUrl(callback, data)
			},
			toAddPage() {
				const _this = this
				// uni.navigateTo({
				// 	url: "./edit/edit?type=add"
				// })
				_this.$refer.setVilit('/pages/address/add/add')
				let callback = function(url) {
					_this.$refs.popupvLogin.open()
				}
				var data = {
					type: 'add'
				}
				_this.$refer.toUrl(callback, data)
			},
			select(row) {
				//是否需要返回地址(从订单确认页跳过来选收货地址)
				if (!this.isSelect) {
					return;
				}
				uni.setStorage({
					key: 'selectAddress',
					data: row,
					success() {
						uni.navigateBack();
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	@import "./index.scss";
</style>
